﻿Imports ADOEntity
Imports System.Threading
Imports System.Globalization
Partial Class Administrator_userEdit
    Inherits Web.UI.BasePage
    Dim sUserEntity As userEntities
    'Dim sPwdHistoryEntity As PwdHistoryEntities
    Dim clsDateFormat As clsDateFormat
    Dim strMsg As String = ""
    Dim clsSessionMenuRight As clsSessionMenuRight
    Dim clsSystemConfig As clsSystemConfig
    Dim strStatusUsePage As String
    Dim strUserName As String
    Dim clsEn As clsEdmUser
    Dim clsDropDown As clsMasterDropDown
    Enum enmAutoDefaultDate
        pwdExpire = 0
        userExpire = 1
        userLock = 2

    End Enum

    Enum enmShowMsg
        chkUser = 1

    End Enum
#Region " Even Page"

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

        lblUserName.Text = Resources.resUser.userName
        lblPassword.Text = Resources.resUser.password
        lblRePassword.Text = Resources.resUser.rePassword
      
        lblDepartmentName.Text = Resources.resUser.DepartmentName
        lblBranchName.Text = Resources.resUser.BranchName

        lblUserRight.Text = "กลุ่มสิทธิ์" 'Resources.resUser.userRight
       
        lblIsUserExpire.Text = Resources.resUser.isUserExpire

       
        lblIsPwdExpire.Text = Resources.resUser.isPwdExpire

        lblPwdExpireDate.Text = Resources.resUser.pwdExpireDate
        
        lblIsPwdLock.Text = Resources.resUser.isPwdLock

     
        lblIsInactive.Text = Resources.resUser.isInactive

        rdolIsInactive.Items(0).Text = Resources.resUser.isInactive_False
        rdolIsInactive.Items(1).Text = Resources.resUser.isInactive_True

        rdolIsUserExpire.Items(0).Text = Resources.resUser.isUserExpire_False
        rdolIsUserExpire.Items(1).Text = Resources.resUser.isUserExpire_True

        

        rdolIsPwdExpire.Items(0).Text = Resources.resUser.isPwdExpire_False
        rdolIsPwdExpire.Items(1).Text = Resources.resUser.isPwdExpire_True

        rdolIsPwdLock.Items(0).Text = Resources.resUser.isPwdLock_False
        rdolIsPwdLock.Items(1).Text = Resources.resUser.isPwdLock_True

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      
        chkIsLogin()
        clsEn = New clsEdmUser
        clsDropDown = New clsMasterDropDown
        sUserEntity = New userEntities
        clsDateFormat = New clsDateFormat

        clsSystemConfig = New clsSystemConfig
        clsSystemConfig.setdefaultValueDate()
        clsSystemConfig.setdefaultValueString()

        Try

            CreateControlMP()
            strStatusUsePage = Request.QueryString("Status")
            strUserName = Request.QueryString("strUserName")
            If Not (IsPostBack) Then
                If Not (strStatusUsePage = "Add") Then
                    PanelUser.Enabled = True
                    btnChkEmployee.Visible = False

                    txtUserName.Enabled = False
                    Dim qry = (From sUser In sUserEntity.SUSER Where sUser.USERNAME = strUserName).ToList
                    txtUserName.Text = qry(0).USERNAME
                    txtFirstName.Text = qry(0).EMPNAME
                    txtLastName.Text = qry(0).EMPLASTNAME

                    txtPassword.Attributes.Add("value", clsEn.DecryPass(qry(0).PASSWORD))
                    hdfChangePwd.Value = clsEn.DecryPass(qry(0).PASSWORD)
                    txtRePassword.Attributes.Add("value", clsEn.DecryPass(qry(0).PASSWORD))

                  
                    txtStartDate.Text = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, qry(0).PWDBEGINDATE)

                    loadDataToDropDown(qry(0).USERRIGHT, qry(0).DEPTCODE, qry(0).ALLCODERIGHT, qry(0).ROLECODE, qry(0).ZONECODE)

                    If (qry(0).MUSTCHANGEPWD = 1) Then
                        chkMustChangePwd.Checked = True
                    Else
                        chkMustChangePwd.Checked = False
                    End If

                    If (qry(0).APPROVERIGHT = 1) Then
                        chkApproveRight.Checked = True
                    Else
                        chkApproveRight.Checked = False
                    End If

                    ' chkMustChangePwd.Checked = qry(0).MUSTCHANGEPWD


                    If Not (qry(0).PWDBEGINDATE Is Nothing) Then
                        txtStartDate.Text = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, qry(0).PWDBEGINDATE)
                        txtStartDate0.Text = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, qry(0).PWDBEGINDATE)
                    Else
                        txtEndDate.Text = ""
                        txtEndDate0.Text = ""
                    End If

                    If Not (qry(0).USEREXPIREDATE Is Nothing) Then
                        txtEndDate.Text = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, qry(0).USEREXPIREDATE)
                        txtEndDate0.Text = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, qry(0).USEREXPIREDATE)
                    Else
                        txtEndDate.Text = ""
                        txtEndDate0.Text = ""
                    End If

                    If Not (qry(0).ISUSEREXPIRE Is Nothing) Then
                        rdolIsUserExpire.SelectedValue = qry(0).ISUSEREXPIRE
                    Else
                        rdolIsUserExpire.SelectedValue = 0
                    End If

                    If Not (qry(0).ISPWDEXPIRE Is Nothing) Then
                        rdolIsPwdExpire.SelectedValue = qry(0).ISPWDEXPIRE
                    Else
                        rdolIsPwdExpire.SelectedValue = 0
                    End If

                    If Not (qry(0).PWDEXPIREDATE Is Nothing) Then
                        txtPwdExpireDate.Text = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, qry(0).PWDEXPIREDATE)
                        txtPwdExpireDate0.Text = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, qry(0).PWDEXPIREDATE)
                    Else
                        txtPwdExpireDate0.Text = ""
                        txtPwdExpireDate.Text = ""
                    End If

                    If Not (qry(0).ISPWDLOCK Is Nothing) Then
                        rdolIsPwdLock.SelectedValue = qry(0).ISPWDLOCK
                    Else
                        rdolIsPwdLock.SelectedValue = 0
                    End If

                    rdolIsInactive.SelectedValue = qry(0).ISINACTIVE
                Else

                    clsSessionMenuRight = Session("ClsMenuRight")

                    btnOK.Enabled = False
                    loadDataToDropDown(Branch:=clsSessionMenuRight.getAllCodeRight)

                    txtPassword.Attributes.Add("value", clsSystemConfig.Password)
                    txtRePassword.Attributes.Add("value", clsSystemConfig.Password)
                    ' txtPwdExpireDate.Text = clsSystemConfig.PwdExpireDateUser
                    txtStartDate.Text = clsSystemConfig.UserBeginDate
                    txtEndDate.Text = clsSystemConfig.userExpireDate

                End If

            End If
            ChkPageStatus(strStatusUsePage)
        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" + ex.Message.Replace("'", "") + "')", True)
        End Try
        'clsEn = Nothing
    End Sub

#End Region

#Region "SetForm"

    Sub CreateControlMP()

        Dim lblForm As Label = CType(Master.FindControl("lblForm"), Label)
        lblForm.Text = "ผู้ใช้งานระบบ"
    End Sub

#End Region

#Region " Even Button "

    'Protected Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click
    '    Dim chkPassSave As Int16 = 0
    '    Dim clsEn As New clsEdmUser
    '    Dim strErrorEncry As String = ""
    '    Dim dataPass As String = ""
    '    Dim clsSessionMenuRight As clsSessionMenuRight = Session("ClsMenuRight")
    '    Dim clsEdmUser As clsEdmUser = New clsEdmUser
    '    sUserEntity = New userEntities
    '    sPwdHistoryEntity = New PwdHistoryEntities

    '    Try
    '        If chkBeforeSave() Then
    '            Dim sUser As New SUSER
    '            Dim SPWDHISTORY As New SPWDHISTORY
    '            If (Request.QueryString("Status") = "Add") Then
    '                sUser.USERNAME = txtUserName.Text.Trim

    '                dataPass = clsEn.converPassLoginToDB(txtPassword.Text)
    '                If (clsEn.ErrExp = "") Then
    '                    sUser.PASSWORD = dataPass
    '                Else
    '                    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" + clsEn.ErrExp + "')", True)
    '                    Exit Sub
    '                End If
    '                sUser.EMPCODE = txtUserName.Text.Trim
    '                sUser.EMPNAME = txtFirstName.Text.Trim
    '                sUser.EMPLASTNAME = txtLastName.Text.Trim
    '                sUser.DEPTCODE = ddlDepartment.SelectedValue
    '                sUser.ALLCODERIGHT = ddlBranch.SelectedValue
    '                sUser.BRANCHCODE = ddlBranch.SelectedValue.Split("/")(1)

    '                sUser.USERRIGHT = ddlUserRight.SelectedValue
    '                sUser.ROLECODE = ddlRoleGroup.SelectedValue

    '                sUser.PWDBEGINDATE = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, dteDateIsChk:=txtStartDate.Text)
    '                sUser.PWDEXPIREDATE = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtPwdExpireDate.Text) 'ได้จาก begindate + เวลาที่ เซ็ตไว้ใน ฐานข้อมูล
    '                sUser.USEREXPIREDATE = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtEndDate.Text) 'auto value
    '                If (chkMustChangePwd.Checked) Then
    '                    sUser.MUSTCHANGEPWD = 1
    '                Else
    '                    sUser.MUSTCHANGEPWD = 0
    '                End If

    '                'auto value
    '                sUser.ISPWDEXPIRE = 0
    '                sUser.ISUSEREXPIRE = 0
    '                sUser.ISPWDLOCK = 0
    '                sUser.CURRENTLOCKNUM = 0
    '                sUser.ISDISABLE = 0

    '                sUser.ISINACTIVE = rdolIsInactive.SelectedValue
    '                If rdolIsInactive.SelectedValue = 0 Then
    '                    sUser.INACTIVEDATE = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, Date.Now)
    '                End If

    '                sUser.CREATEDATE = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, Date.Now) 'auto
    '                sUser.CREATEBY = clsSessionMenuRight.getUserRight 'auto value คนที่เข้ามาสร้าง user นี้ในระบบ
    '                sUser.UPDATEDATE = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, Date.Now)
    '                sUser.UPDATEBY = clsSessionMenuRight.getUserRight 'auto value คนที่เข้ามาสร้าง user นี้ในระบบ

    '                sUserEntity.AddToSUSER(sUser)
    '                sUserEntity.SaveChanges()
    '                '  If (clsEdmUser.SavePwdHistory(txtUserName.Text, dataPass, clsSessionMenuRight.getUserRight)) Then

    '                'Else
    '                '  ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" + "เกินข้อผิดพลาด" + "')", True)
    '                '  Exit Sub
    '                ' End If

    '                chkPassSave = 1


    '            ElseIf (Request.QueryString("Status") = "Edit") Then
    '                Dim qry = (From User In sUserEntity.SUSER Where User.USERNAME = txtUserName.Text).ToList
    '                If Not (txtPassword.Text.Trim = "") Then
    '                    dataPass = clsEn.converPassLoginToDB(txtPassword.Text)
    '                    If (clsEn.ErrExp = "") Then
    '                        qry(0).PASSWORD = dataPass
    '                    Else
    '                        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" + clsEn.ErrExp + "')", True)
    '                        Exit Sub
    '                    End If
    '                End If
    '                'qry(0).USERDESC = txtUserDesc.Text

    '                qry(0).EMPCODE = txtUserName.Text.Trim
    '                qry(0).EMPNAME = txtFirstName.Text.Trim
    '                qry(0).EMPLASTNAME = txtLastName.Text.Trim
    '                qry(0).DEPTCODE = ddlDepartment.SelectedValue
    '                qry(0).ALLCODERIGHT = ddlBranch.SelectedValue
    '                qry(0).BRANCHCODE = ddlBranch.SelectedValue.Split("/")(1)


    '                If (txtStartDate.Enabled = True) Then
    '                    qry(0).PWDBEGINDATE = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, dteDateIsChk:=txtStartDate.Text)
    '                End If
    '                qry(0).PWDEXPIREDATE = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtPwdExpireDate.Text) 'ได้จาก begindate + เวลาที่ เซ็ตไว้ใน ฐานข้อมูล
    '                qry(0).USEREXPIREDATE = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtEndDate.Text) 'auto value
    '                If (chkMustChangePwd.Checked) Then
    '                    qry(0).MUSTCHANGEPWD = 1
    '                Else
    '                    qry(0).MUSTCHANGEPWD = 0
    '                End If

    '                qry(0).UPDATEDATE = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, Date.Now)
    '                qry(0).UPDATEBY = clsSessionMenuRight.getUserRight

    '                sUser.ISINACTIVE = rdolIsInactive.SelectedValue
    '                If rdolIsInactive.SelectedValue = 0 Then
    '                    sUser.INACTIVEDATE = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, Date.Now)
    '                End If
    '                'If Not (hdfChangePwd.Value = txtPassword.Text.Trim) Then
    '                'If (clsEdmUser.SavePwdHistory(txtUserName.Text, dataPass, clsSessionMenuRight.getUserRight)) Then
    '                'sUserEntity.SaveChanges()
    '                'Else
    '                'ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" + "เกินข้อผิดพลาด" + "')", True)
    '                ' Exit Sub
    '                'End If
    '                ' Else
    '                'sUserEntity.SaveChanges()
    '                'End If
    '                sUserEntity.SaveChanges()
    '                chkPassSave = 1
    '                qry = Nothing
    '                sUserEntity = Nothing


    '            ElseIf (Request.QueryString("Status") = "UserExpire") Then
    '                Dim qry = (From User In sUserEntity.SUSER Where User.USERNAME = txtUserName.Text).ToList

    '                qry(0).ISUSEREXPIRE = rdolIsUserExpire.SelectedValue
    '                ' qry(0).USEREXPIREDATE = txtUserExpireDate.Text

    '                qry(0).UPDATEDATE = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, Date.Now)
    '                qry(0).UPDATEBY = clsSessionMenuRight.getUserRight

    '                sUserEntity.SaveChanges()
    '                chkPassSave = 1
    '                qry = Nothing
    '                sUserEntity = Nothing

    '            ElseIf (Request.QueryString("Status") = "PwdExpire") Then
    '                Dim qry = (From User In sUserEntity.SUSER Where User.USERNAME = txtUserName.Text).ToList
    '                If Not (txtPassword.Text.Trim = "") Then
    '                    dataPass = clsEn.converPassLoginToDB(txtPassword.Text)
    '                    If (clsEn.ErrExp = "") Then
    '                        qry(0).PASSWORD = dataPass
    '                    Else
    '                        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" + clsEn.ErrExp + "')", True)
    '                        Exit Sub
    '                    End If
    '                End If
    '                If (chkMustChangePwd.Checked) Then
    '                    qry(0).MUSTCHANGEPWD = 1
    '                Else
    '                    qry(0).MUSTCHANGEPWD = 0
    '                End If

    '                qry(0).ISPWDEXPIRE = rdolIsPwdExpire.SelectedValue
    '                qry(0).PWDEXPIREDATE = txtPwdExpireDate.Text

    '                qry(0).UPDATEDATE = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, Date.Now)
    '                qry(0).UPDATEBY = clsSessionMenuRight.getUserRight

    '                If Not (hdfChangePwd.Value = txtPassword.Text.Trim) Then

    '                    If (clsEdmUser.SavePwdHistory(txtUserName.Text, dataPass, clsSessionMenuRight.getUserRight)) Then
    '                        sUserEntity.SaveChanges()
    '                    Else
    '                        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" + "เกินข้อผิดพลาด" + "')", True)
    '                        Exit Sub
    '                    End If
    '                Else
    '                    sUserEntity.SaveChanges()
    '                End If

    '                ' sPwdHistoryEntity.AddToSPWDHISTORY(SPWDHISTORY)
    '                'sUserEntity.SaveChanges()
    '                sPwdHistoryEntity.SaveChanges()
    '                chkPassSave = 1
    '                qry = Nothing
    '                sUserEntity = Nothing
    '            ElseIf (Request.QueryString("Status") = "PwdLock") Then
    '                Dim qry = (From User In sUserEntity.SUSER Where User.USERNAME = txtUserName.Text).ToList
    '                If Not (txtPassword.Text.Trim = "") Then
    '                    dataPass = clsEn.converPassLoginToDB(txtPassword.Text)
    '                    If (clsEn.ErrExp = "") Then
    '                        qry(0).PASSWORD = dataPass
    '                    Else
    '                        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" + clsEn.ErrExp + "')", True)
    '                        Exit Sub
    '                    End If
    '                End If
    '                If (chkMustChangePwd.Checked) Then
    '                    qry(0).MUSTCHANGEPWD = 1
    '                Else
    '                    qry(0).MUSTCHANGEPWD = 0
    '                End If

    '                qry(0).ISPWDLOCK = rdolIsPwdLock.SelectedValue
    '                qry(0).UPDATEDATE = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, Date.Now)
    '                qry(0).UPDATEBY = clsSessionMenuRight.getUserRight

    '                If Not (hdfChangePwd.Value = txtPassword.Text.Trim) Then
    '                    If (clsEdmUser.SavePwdHistory(txtUserName.Text, dataPass, clsSessionMenuRight.getUserRight)) Then
    '                        sUserEntity.SaveChanges()
    '                    Else
    '                        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" + "เกินข้อผิดพลาด" + "')", True)
    '                        Exit Sub
    '                    End If
    '                Else
    '                    sUserEntity.SaveChanges()
    '                End If

    '                'sPwdHistoryEntity.AddToSPWDHISTORY(SPWDHISTORY)
    '                'sUserEntity.SaveChanges()
    '                'sPwdHistoryEntity.SaveChanges()
    '                chkPassSave = 1
    '                qry = Nothing
    '                sUserEntity = Nothing

    '            End If
    '            clsSessionMenuRight = Nothing
    '        Else
    '            ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" + strMsg + "')", True)
    '            Exit Sub
    '        End If
    '    Catch ex As System.Data.UpdateException
    '        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" + ex.InnerException.Message + "')", True)
    '        chkPassSave = 0
    '    Catch ex As Exception
    '        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('บันทึกผิดพลาด')", True)
    '        chkPassSave = 0
    '    End Try

    '    If (chkPassSave = 1) Then

    '        If Request.QueryString("Status") = "Add" Then
    '            Response.Redirect("userList.aspx")
    '        ElseIf Request.QueryString("Status") = "Edit" Then
    '            Response.Redirect("userList.aspx")
    '        ElseIf Request.QueryString("Status") = "PwdExpire" Then

    '        ElseIf Request.QueryString("Status") = "UserExpire" Then
    '            Response.Redirect("userExpire.aspx")

    '        ElseIf Request.QueryString("Status") = "PwdLock" Then
    '            Response.Redirect("userPwdLock.aspx")
    '        End If
    '    End If

    'End Sub

    Protected Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Dim chkPassSave As Int16 = 0
        Dim clsEn As New clsEdmUser
        Dim strErrorEncry As String = ""
        Dim dataPass As String = ""
        Dim clsSessionMenuRight As clsSessionMenuRight = Session("ClsMenuRight")
        Dim clsEdmUser As clsEdmUser = New clsEdmUser
        sUserEntity = New userEntities
        'sPwdHistoryEntity = New PwdHistoryEntities

        Try
            If chkBeforeSave() Then
                Dim sUser As New SUSER
                Dim SPWDHISTORY As New SPWDHISTORY
                If (Request.QueryString("Status") = "Add") Then
                    sUser.USERNAME = txtUserName.Text.Trim

                    dataPass = clsEn.converPassLoginToDB(txtPassword.Text)
                    If (clsEn.ErrExp = "") Then
                        sUser.PASSWORD = dataPass
                    Else
                        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" + clsEn.ErrExp + "')", True)
                        Exit Sub
                    End If
                    sUser.EMPCODE = txtUserName.Text.Trim
                    sUser.EMPNAME = txtFirstName.Text.Trim
                    sUser.EMPLASTNAME = txtLastName.Text.Trim
                    sUser.DEPTCODE = ddlDepartment.SelectedValue
                    sUser.ALLCODERIGHT = ddlBranch.SelectedValue
                    'If Not (ddlBranch.SelectedValue = "0") Then
                    '    sUser.BRANCHCODE = ddlBranch.SelectedValue.Split("/")(1)
                    '    sUser.ZONECODE = ddlBranch.SelectedValue.Split("/")(2)
                    'End If
                    sUser.BRANCHCODE = ddlBranch.SelectedValue.Split("/")(1)
                    sUser.ZONECODE = ddlBranch.SelectedValue.Split("/")(2)
                    sUser.USERRIGHT = ddlUserRight.SelectedValue
                    sUser.ROLECODE = ddlRoleGroup.SelectedValue

                    sUser.PWDBEGINDATE = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, dteDateIsChk:=txtStartDate.Text)
                    sUser.PWDEXPIREDATE = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtPwdExpireDate.Text) 'ได้จาก begindate + เวลาที่ เซ็ตไว้ใน ฐานข้อมูล
                    sUser.USEREXPIREDATE = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtEndDate.Text) 'auto value

                    If (chkMustChangePwd.Checked) Then
                        sUser.MUSTCHANGEPWD = 1
                    Else
                        sUser.MUSTCHANGEPWD = 0
                    End If

                    If (chkApproveRight.Checked) Then
                        sUser.APPROVERIGHT = 1
                    Else
                        sUser.APPROVERIGHT = 0
                    End If

                    'auto value
                    sUser.ISPWDEXPIRE = 0
                    sUser.ISUSEREXPIRE = 0
                    sUser.ISPWDLOCK = 0
                    sUser.CURRENTLOCKNUM = 0
                    sUser.ISDISABLE = 0

                    sUser.ISINACTIVE = rdolIsInactive.SelectedValue
                    If rdolIsInactive.SelectedValue = 0 Then
                        sUser.INACTIVEDATE = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, Date.Now)
                    End If

                    sUser.CREATEDATE = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, Date.Now) 'auto
                    sUser.CREATEBY = clsSessionMenuRight.getUserName 'auto value คนที่เข้ามาสร้าง user นี้ในระบบ
                    sUser.UPDATEDATE = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, Date.Now)
                    sUser.UPDATEBY = clsSessionMenuRight.getUserName 'auto value คนที่เข้ามาสร้าง user นี้ในระบบ
                    sUser.ISLOGIN = 0
                    sUserEntity.AddToSUSER(sUser)
                    sUserEntity.SaveChanges()
                    chkPassSave = 1

                    SaveLogNew()
                ElseIf (Request.QueryString("Status") = "Edit") Then
                    Dim qry = (From User In sUserEntity.SUSER Where User.USERNAME = txtUserName.Text).ToList
                    If Not (txtPassword.Text.Trim = "") Then
                        dataPass = clsEn.converPassLoginToDB(txtPassword.Text)
                        If (clsEn.ErrExp = "") Then
                            qry(0).PASSWORD = dataPass
                        Else
                            ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" + clsEn.ErrExp + "')", True)
                            Exit Sub
                        End If
                    End If
                    'qry(0).USERDESC = txtUserDesc.Text

                    qry(0).EMPCODE = txtUserName.Text.Trim
                    qry(0).EMPNAME = txtFirstName.Text.Trim
                    qry(0).EMPLASTNAME = txtLastName.Text.Trim
                    qry(0).DEPTCODE = ddlDepartment.SelectedValue
                    qry(0).ALLCODERIGHT = ddlBranch.SelectedValue
                    'If Not (ddlBranch.SelectedValue = "0") Then
                    '    qry(0).BRANCHCODE = ddlBranch.SelectedValue.Split("/")(1)
                    '    qry(0).ZONECODE = ddlBranch.SelectedValue.Split("/")(2)
                    'End If
                    qry(0).BRANCHCODE = ddlBranch.SelectedValue.Split("/")(1)
                    qry(0).ZONECODE = ddlBranch.SelectedValue.Split("/")(2)
                    qry(0).USERRIGHT = ddlUserRight.SelectedValue
                    qry(0).ROLECODE = ddlRoleGroup.SelectedValue

                    If (txtStartDate.Enabled = True) Then
                        qry(0).PWDBEGINDATE = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, dteDateIsChk:=txtStartDate.Text)
                    End If

                    '  qry(0).ISPWDEXPIRE = rdolIsPwdExpire.SelectedValue
                    qry(0).ISPWDEXPIRE = 0
                    qry(0).ISUSEREXPIRE = 0
                    qry(0).PWDEXPIREDATE = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtPwdExpireDate.Text) 'ได้จาก begindate + เวลาที่ เซ็ตไว้ใน ฐานข้อมูล
                    qry(0).ISPWDLOCK = rdolIsPwdLock.SelectedValue

                    If rdolIsPwdLock.SelectedValue = 0 Then
                        qry(0).CURRENTLOCKNUM = 0
                    End If

                    qry(0).PWDLOCKDATE = Nothing

                    qry(0).USEREXPIREDATE = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtEndDate.Text) 'auto value

                    If (chkMustChangePwd.Checked) Then
                        qry(0).MUSTCHANGEPWD = 1
                    Else
                        qry(0).MUSTCHANGEPWD = 0
                    End If

                    If (chkApproveRight.Checked) Then
                        qry(0).APPROVERIGHT = 1
                    Else
                        qry(0).APPROVERIGHT = 0
                    End If

                    qry(0).UPDATEDATE = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, Date.Now)
                    qry(0).UPDATEBY = clsSessionMenuRight.getUserName

                    qry(0).ISINACTIVE = rdolIsInactive.SelectedValue
                    If rdolIsInactive.SelectedValue = 0 Then
                        sUser.INACTIVEDATE = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, Date.Now)
                    End If
                    sUserEntity.SaveChanges()
                    chkPassSave = 1
                    qry = Nothing
                    sUserEntity = Nothing

                    SaveLogEdit()
                ElseIf (Request.QueryString("Status") = "2") Then
                    Dim qry = (From User In sUserEntity.SUSER Where User.USERNAME = txtUserName.Text).ToList

                    qry(0).ISUSEREXPIRE = rdolIsUserExpire.SelectedValue
                    qry(0).PWDBEGINDATE = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtStartDate.Text)
                    qry(0).USEREXPIREDATE = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtEndDate.Text)
                    qry(0).UPDATEDATE = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, Date.Now)
                    qry(0).UPDATEBY = clsSessionMenuRight.getUserName

                    sUserEntity.SaveChanges()
                    chkPassSave = 1
                    qry = Nothing
                    sUserEntity = Nothing
                    SaveLogEdit()
                ElseIf (Request.QueryString("Status") = "3") Then
                    Dim qry = (From User In sUserEntity.SUSER Where User.USERNAME = txtUserName.Text).ToList

                    dataPass = clsEn.converPassLoginToDB(txtPassword.Text)
                    If (clsEn.ErrExp = "") Then
                        qry(0).PASSWORD = dataPass
                    Else
                        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" + clsEn.ErrExp + "')", True)
                        Exit Sub
                    End If

                    If (chkMustChangePwd.Checked) Then
                        qry(0).MUSTCHANGEPWD = 1
                    Else
                        qry(0).MUSTCHANGEPWD = 0
                    End If

                    qry(0).ISPWDEXPIRE = rdolIsPwdExpire.SelectedValue

                    qry(0).PWDEXPIREDATE = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtPwdExpireDate.Text)

                    qry(0).UPDATEDATE = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, Date.Now)
                    qry(0).UPDATEBY = clsSessionMenuRight.getUserName

                    sUserEntity.SaveChanges()
                    chkPassSave = 1
                    qry = Nothing
                    sUserEntity = Nothing
                    SaveLogEdit()
                ElseIf (Request.QueryString("Status") = "1") Then
                    Dim qry = (From User In sUserEntity.SUSER Where User.USERNAME = txtUserName.Text).ToList


                    dataPass = clsEn.converPassLoginToDB(txtPassword.Text)
                    If (clsEn.ErrExp = "") Then
                        qry(0).PASSWORD = dataPass
                    Else
                        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" + clsEn.ErrExp + "')", True)
                        Exit Sub
                    End If

                    If (chkMustChangePwd.Checked) Then
                        qry(0).MUSTCHANGEPWD = 1
                    Else
                        qry(0).MUSTCHANGEPWD = 0
                    End If

                    qry(0).ISPWDLOCK = rdolIsPwdLock.SelectedValue
                    qry(0).PWDLOCKDATE = Nothing
                    qry(0).CURRENTLOCKNUM = 0
                    qry(0).PWDEXPIREDATE = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtPwdExpireDate.Text)
                    qry(0).UPDATEDATE = clsDateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, Date.Now)
                    qry(0).UPDATEBY = clsSessionMenuRight.getUserName


                    sUserEntity.SaveChanges()
                    chkPassSave = 1
                    qry = Nothing
                    sUserEntity = Nothing
                    SaveLogEdit()
                End If
                clsSessionMenuRight = Nothing
            Else
                Dim strTempPwd As String = txtPassword.Text
                Dim strTemprePwd As String = txtRePassword.Text
                txtPassword.Attributes.Clear()
                txtRePassword.Attributes.Clear()
                txtPassword.Attributes.Add("value", strTempPwd)
                txtRePassword.Attributes.Add("value", strTemprePwd)
                ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" + strMsg + "')", True)
                Exit Sub
            End If

        Catch ex As System.Data.UpdateException
            WriteError(ex, "btnOK_Click", "userEdit")
            ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" + ex.InnerException.Message + "')", True)
            chkPassSave = 0
        Catch ex As Exception
            WriteError(ex, "btnOK_Click", "userEdit")
            ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('บันทึกผิดพลาด')", True)
            chkPassSave = 0
        End Try

        If (chkPassSave = 1) Then

            If Request.QueryString("Status") = "Add" Then
                Response.Redirect("userList.aspx")
            ElseIf Request.QueryString("Status") = "Edit" Then
                Response.Redirect("userList.aspx")
            ElseIf Request.QueryString("Status") = "1" Then
                Response.Redirect("userExpire.aspx")
            ElseIf Request.QueryString("Status") = "2" Then
                Response.Redirect("userExpire.aspx")

            ElseIf Request.QueryString("Status") = "3" Then
                Response.Redirect("userExpire.aspx")
            End If
        End If

    End Sub


    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        If (strStatusUsePage = "Add" Or strStatusUsePage = "Edit") Then
            Response.Redirect("userList.aspx")
        ElseIf (strStatusUsePage = "1") Then
            Response.Redirect("userExpire.aspx")
        ElseIf (strStatusUsePage = "2") Then
            Response.Redirect("userExpire.aspx")
        ElseIf (strStatusUsePage = "3") Then
            Response.Redirect("userExpire.aspx")
        End If


    End Sub
#End Region

#Region " Function "
    Private Sub loadDataToDropDown(Optional ByVal UserRight As String = "", Optional ByVal Dept As String = "", _
                                   Optional ByVal Branch As String = "", Optional ByVal RoleGroup As String = "", Optional ByVal Zone As String = "")

        Try
            Dim clsDropDown As New clsMasterDropDown

            clsSessionMenuRight = Session("ClsMenuRight")

            clsDropDown.setDeptToDdl(ddlDepartment, Dept, _
                                     strDeptCode:=clsSessionMenuRight.getDeptCode, _
                                     strRoleCode:=clsSessionMenuRight.getRoleGroup)

            If Not (ddlDepartment.SelectedValue = "0") Then
                ddlBranch.Enabled = True
                clsDropDown.setZoneToDdl(ddlZone, _
                                         strDeptCode:=ddlDepartment.SelectedValue, _
                                         selectData:=Zone, _
                                         strRoleCode:=clsSessionMenuRight.getRoleGroup, _
                                         strZoneCode:=clsSessionMenuRight.getZoneCode)
            Else
                ddlZone.Enabled = False
                ddlBranch.Enabled = False
            End If

            If Not (ddlDepartment.SelectedValue = "0") Then
                ddlBranch.Enabled = True
                clsDropDown.setBranchUseAllCodeToDdl(ddlBranch, _
                                                     strDeptCode:=ddlDepartment.SelectedValue, _
                                                     selectData:=Branch, _
                                                     strBranchCode:=clsSessionMenuRight.getBranchCode, _
                                                     strRoleCode:=clsSessionMenuRight.getRoleGroup, _
                                                     strZoneCode:=clsSessionMenuRight.getZoneCode)
            Else

                ddlBranch.Enabled = False
            End If



            If Not (ddlBranch.SelectedIndex = 0 Or ddlBranch.SelectedIndex = -1) Then
                Dim tempZoneCode As String = ddlBranch.SelectedValue.Split("/")(2)

                If Not (tempZoneCode = "ZZZ") Then

                    clsDropDown.setRoleToDdl(ddlRoleGroup, RoleGroup, clsSessionMenuRight.getRoleGroup, tempZoneCode)
                Else
                    clsDropDown.setRoleToDdl(ddlRoleGroup, RoleGroup, clsSessionMenuRight.getRoleGroup)
                End If
            Else
                clsDropDown.setRoleToDdl(ddlRoleGroup, selectData:=RoleGroup, _
                                               strRoleCode:=clsSessionMenuRight.getRoleGroup)
            End If



            If Not (ddlRoleGroup.SelectedValue = "0" And ddlDepartment.SelectedValue = "0") Then
                ddlUserRight.Enabled = True

                clsDropDown.setUserRightToDd(ddlUserRight, ddlRoleGroup.SelectedValue, UserRight, ddlDepartment.SelectedValue)

            Else
                ddlUserRight.Enabled = False
            End If




            'If (RoleGroup = "") Then
            '    clsDropDown.SetRoleGroupDDL(ddlRoleGroup)
            'Else
            '    clsDropDown.SetRoleGroupDDL(ddlRoleGroup, ddlBranch.SelectedValue, RoleGroup)
            'End If

            'If (clsSessionMenuRight.getRoleGroup = "2") Then

            '    If (Dept = "") Then
            '        clsDropDown.setDeptToDdl(ddlDepartment)
            '    Else
            '        clsDropDown.setDeptToDdl(ddlDepartment, Dept)
            '    End If

            '    If (Branch = "") Then
            '        clsDropDown.setBranchMergeField(ddlBranch, 0)
            '    Else
            '        clsDropDown.setBranchMergeField(ddlBranch, strCondition:=ddlDepartment.SelectedValue, selectData:=Branch)
            '    End If
            '    If (RoleGroup = "") Then
            '        clsDropDown.SetRoleGroupDDL(ddlRoleGroup)
            '    Else
            '        clsDropDown.SetRoleGroupDDL(ddlRoleGroup, ddlBranch.SelectedValue, RoleGroup)
            '    End If
            'ElseIf (clsSessionMenuRight.getRoleGroup = "3") Then
            '    If (UserRight = "") Then
            '        clsDropDown.setUserRightToDdl(ddlUserRight, clsSessionMenuRight.getUserRight)
            '    Else
            '        clsDropDown.setUserRightToDdl(ddlUserRight, UserRight)
            '    End If
            '    ddlUserRight.Enabled = False
            '    If (Dept = "") Then
            '        clsDropDown.setDeptToDdl(ddlDepartment, clsSessionMenuRight.getDeptCode)
            '    Else
            '        clsDropDown.setDeptToDdl(ddlDepartment, Dept)
            '    End If
            '    ddlDepartment.Enabled = False

            '    If (Branch = "") Then
            '        clsDropDown.setBranchMergeField(ddlBranch, 0)
            '    Else
            '        clsDropDown.setBranchMergeField(ddlBranch, ddlDepartment.SelectedValue, Branch)
            '    End If
            '    If (RoleGroup = ""
            clsDropDown = Nothing
        Catch ex As Exception
            WriteError(ex, "loadDataToDropDown", "userEdit")
        End Try
     
    End Sub
    Private Sub ChkPageStatusBackEnablePanal(ByVal strStatus As String)
        If (strStatus.Contains("Add")) Then
            If (PanelUser.Enabled = False) Then
                'เซ็ต pwdExpire 
                txtPwdExpireDate.Visible = False
                txtPwdExpireDate0.Visible = True

                txtStartDate.Visible = False
                lblRoleGroup1.Visible = False
                txtEndDate.Visible = False

                txtStartDate0.Visible = True
                lblRoleGroup2.Visible = True
                txtEndDate0.Visible = True
            Else
                txtPwdExpireDate.Visible = True
                txtPwdExpireDate0.Visible = False
                txtStartDate.Visible = True
                lblRoleGroup1.Visible = True
                txtEndDate.Visible = True

                txtStartDate0.Visible = False
                lblRoleGroup2.Visible = False
                txtEndDate0.Visible = False
            End If
        End If
    End Sub

    Private Sub ChkPageStatus(ByVal strStatus As String)
        If (strStatus.Contains("Add")) Then
          
            rdolIsPwdLock.Enabled = False
            rdolIsPwdLock.Items(0).Selected = True
            rdolIsPwdLock.Items(1).Selected = False

            rdolIsPwdExpire.Enabled = False
            rdolIsPwdExpire.Items(0).Selected = True
            rdolIsPwdExpire.Items(1).Selected = False



            rdolIsUserExpire.Enabled = False
            rdolIsUserExpire.Items(0).Selected = True
            rdolIsUserExpire.Items(1).Selected = False

            ' เซ็ต user StartDate ,EndDate  

            'เซ็ต pwdExpire 
            If (PanelUser.Enabled = False) Then
                'เซ็ต pwdExpire 
                txtPwdExpireDate.Visible = False
                txtPwdExpireDate0.Visible = True

                txtStartDate.Visible = False
                lblRoleGroup1.Visible = False
                txtEndDate.Visible = False

                txtStartDate0.Visible = True
                lblRoleGroup2.Visible = True
                txtEndDate0.Visible = True
            Else
                txtPwdExpireDate.Visible = True
                txtPwdExpireDate0.Visible = False
                txtStartDate.Visible = True
                lblRoleGroup1.Visible = True
                txtEndDate.Visible = True

                txtStartDate0.Visible = False
                lblRoleGroup2.Visible = False
                txtEndDate0.Visible = False
            End If


        ElseIf strStatus.Contains("Edit") Then


            rdolIsPwdLock.Enabled = False
            'rdolIsPwdLock.Items(0).Selected = False
            'rdolIsPwdLock.Items(1).Selected = False

            rdolIsPwdExpire.Enabled = False
            'rdolIsPwdExpire.Items(0).Selected = False
            'rdolIsPwdExpire.Items(1).Selected = False



            rdolIsUserExpire.Enabled = False
            'rdolIsUserExpire.Items(0).Selected = False
            'rdolIsUserExpire.Items(1).Selected = False

            ' เซ็ต user StartDate ,EndDate  
           
            If (PanelUser.Enabled = False) Then
                'เซ็ต pwdExpire 
                txtPwdExpireDate.Visible = False
                txtPwdExpireDate0.Visible = True

                txtStartDate.Visible = False
                lblRoleGroup1.Visible = False
                txtEndDate.Visible = False

                txtStartDate0.Visible = True
                lblRoleGroup2.Visible = True
                txtEndDate0.Visible = True
            Else
                txtPwdExpireDate.Visible = True
                txtPwdExpireDate0.Visible = False
                txtStartDate.Visible = True
                lblRoleGroup1.Visible = True
                txtEndDate.Visible = True

                txtStartDate0.Visible = False
                lblRoleGroup2.Visible = False
                txtEndDate0.Visible = False
            End If


        Else

            txtUserName.Enabled = False
            txtFirstName.Enabled = False
            txtLastName.Enabled = False

            txtPassword.Enabled = False
            txtRePassword.Enabled = False
            
            ddlDepartment.Enabled = False
            ddlUserRight.Enabled = False
            ddlBranch.Enabled = False
            ddlRoleGroup.Enabled = False
            rdolIsInactive.Enabled = False

            If (strStatus.Contains("3")) Then
                txtPassword.Enabled = True
                txtRePassword.Enabled = True
                chkMustChangePwd.Enabled = True
                rdolIsPwdExpire.Enabled = True

                txtPwdExpireDate.Enabled = True
                txtPwdExpireDate0.Visible = False

                txtStartDate.Visible = False
                txtStartDate0.Visible = True
                txtStartDate0.Enabled = False

                txtEndDate.Visible = False
                txtEndDate0.Visible = True
                txtEndDate0.Enabled = False

                rdolIsUserExpire.Enabled = False
                'rdolIsUserExpire.Items(0).Selected = False
                'rdolIsUserExpire.Items(1).Selected = False
                rdolIsInactive.Enabled = False
                'rdolIsInactive.Items(0).Selected = False
                'rdolIsInactive.Items(1).Selected = False

                rdolIsPwdLock.Enabled = False
                'rdolIsPwdLock.Items(0).Selected = False
                'rdolIsPwdLock.Items(1).Selected = False

            ElseIf (strStatus.Contains("2")) Then

                rdolIsUserExpire.Enabled = True

                txtStartDate.Visible = True
                txtStartDate.Enabled = True
                txtStartDate0.Visible = False
                txtStartDate0.Enabled = False

                txtEndDate.Visible = True
                txtEndDate.Enabled = True
                txtEndDate0.Visible = False
                txtEndDate0.Enabled = False


                rdolIsInactive.Enabled = False
                'rdolIsInactive.Items(0).Selected = False
                'rdolIsInactive.Items(1).Selected = False


                rdolIsPwdLock.Items(1).Selected = False

                rdolIsPwdExpire.Enabled = False
                'rdolIsPwdExpire.Items(0).Selected = False
                'rdolIsPwdExpire.Items(1).Selected = False

                txtPwdExpireDate.Visible = False
                txtPwdExpireDate0.Visible = True
                txtPwdExpireDate0.Enabled = False

                rdolIsPwdLock.Enabled = False
                'rdolIsPwdLock.Items(0).Selected = False
                'rdolIsPwdLock.Items(1).Selected = False
                chkMustChangePwd.Enabled = False


            ElseIf (strStatus.Contains("1")) Then


                rdolIsUserExpire.Enabled = False
                'rdolIsUserExpire.Items(0).Selected = False
                'rdolIsUserExpire.Items(1).Selected = False



                rdolIsPwdExpire.Enabled = False
                'rdolIsPwdExpire.Items(0).Selected = False
                'rdolIsPwdExpire.Items(1).Selected = False



                rdolIsInactive.Enabled = False
                'rdolIsInactive.Items(0).Selected = False
                'rdolIsInactive.Items(1).Selected = False


                txtPwdExpireDate.Visible = True
                txtPwdExpireDate0.Visible = False
                txtPwdExpireDate0.Enabled = False

                rdolIsPwdLock.Enabled = True
                txtPassword.Enabled = True
                txtRePassword.Enabled = True
                chkMustChangePwd.Enabled = True


                txtStartDate.Visible = False
                txtStartDate0.Visible = True
                txtStartDate0.Enabled = False

                txtEndDate.Visible = False
                txtEndDate0.Visible = True
                txtEndDate0.Enabled = False

            End If
        End If
    End Sub

    Private Function chkBeforeSave() As Boolean
        Dim result As Boolean = True
        Dim strStatus As String = Request.QueryString("")
        'If (txtUserName.Text = "") Then
        '    result = False
        '    lblChkUserName.Text = String.Format(Resources.resValidate.Validate_TextBox, Resources.resUser.userName)
        '    lblChkUserName.Visible = True
        'Else
        '    'If (txtUserName.Text.Length > 4) Then

        '    '    If (strStatus = "Add") Then
        '    '        Dim chkPk = (From tblUser In sUserEntity.SUSER Where tblUser.USERNAME.ToUpper = txtUserName.Text.Trim.ToUpper).ToList
        '    '        If Not (chkPk.Count = 0) Then
        '    '            result = False
        '    '            lblChkUserName.Text = Resources.resValidate.validate_UserName
        '    '            lblChkUserName.Visible = True
        '    '        Else
        '    '            lblChkUserName.Visible = False
        '    '        End If
        '    '        chkPk = Nothing
        '    '    End If


        '    'Else
        '    '    result = False
        '    '    lblChkUserName.Text = String.Format(Resources.resValidate.validate_TextLength, Resources.resUser.userName)
        '    '    lblChkUserName.Visible = True
        '    'End If

        'End If



        'If (ddlDepartment.SelectedIndex = 0) Then
        '    result = False
        '    lblChkPosName.Visible = True
        'Else
        '    lblChkPosName.Visible = False
        'End If

        If (txtFirstName.Text.Trim = "") Then
            result = False
            strMsg = strMsg + String.Format(Resources.resValidateUser.NotFirstName) + "\n"
        End If

        If (txtLastName.Text.Trim = "") Then
            result = False
            strMsg = strMsg + String.Format(Resources.resValidateUser.NotLastName) + "\n"
        End If

        If (ddlUserRight.SelectedValue = "0") Then
            result = False
            strMsg = strMsg + String.Format(Resources.resValidateUser.NotUserRight) + "\n"
        End If

        If (ddlDepartment.SelectedValue = "0") Then
            result = False
            strMsg = strMsg + String.Format(Resources.resValidateUser.NotDepartment) + "\n"
        End If

        If (ddlRoleGroup.SelectedValue = "0") Then
            result = False
            strMsg = strMsg + String.Format(Resources.resValidateUser.NotRoleGroup) + "\n"
        End If

        If (ddlBranch.SelectedValue = "0") Then
            result = False
            strMsg = strMsg + String.Format(Resources.resValidateUser.NotBranch) + "\n"
        End If

        If chkPassword() = False Then
            result = False
        End If

        'If txtPwdExpireDate.Text.Trim = "" Then
        '    result = False
        '    strMsg = strMsg + String.Format(Resources.resValidateUser.NotPwdExpireDate) + "\n"
        'Else
        '    If clsDateFormat.ChkformatDate(txtPwdExpireDate.Text.Trim) = False Then
        '        result = False
        '        strMsg = strMsg + String.Format(Resources.resValidateUser.PwdExpireDateFormat) + "\n"
        '    Else
        '        Dim intDay As Integer = difDate(Date.Now, txtPwdExpireDate.Text.Trim)

        '        If (intDay <= 0) Then
        '            result = False
        '            strMsg = strMsg + "กรุณากรอกวันที่รหัสผ่านหมดอายุให้มากกว่าวันที่ปัจจุบัน" + "\n"
        '        End If


        '    End If
        'End If

        If txtStartDate.Text.Trim = "" Then
            result = False
            strMsg = strMsg + String.Format(Resources.resValidateUser.NotUserStartDate) + "\n"
        Else
            If clsDateFormat.ChkformatDate(txtStartDate.Text.Trim) = False Then
                result = False
                strMsg = strMsg + String.Format(Resources.resValidateUser.UserStartDateFormat) + "\n"
            End If
        End If


        If txtEndDate.Text.Trim = "" Then
            result = False
            strMsg = strMsg + String.Format(Resources.resValidateUser.NotUserEndDate) + "\n"
        Else
            If clsDateFormat.ChkformatDate(txtEndDate.Text.Trim) = False Then
                result = False
                strMsg = strMsg + String.Format(Resources.resValidateUser.UserEndDateFormat) + "\n"
            Else
                Dim intDay As Integer = difDate(Date.Now, txtEndDate.Text.Trim)

                If (intDay <= 0) Then
                    result = False
                    strMsg = strMsg + "กรุณากรอกวันที่สิ้นสุดให้มากกว่าวันที่ปัจจุบัน" + "\n"
                End If


            End If
        End If

        Return result

    End Function

    Private Function difDate(ByVal beginDate As Date, ByVal endDate As Date) As Integer
        ' Dim blnChkDate As Boolean = False
        Dim dateDif As Integer
        Try
            dateDif = DateDiff(DateInterval.Day, Convert.ToDateTime(beginDate.ToShortDateString), Convert.ToDateTime(endDate.ToShortDateString))
        Catch ex As Exception
            Throw ex
        End Try

        Return dateDif
    End Function


    Private Function chkPassword() As Boolean
        Dim chkstatus As Boolean = True
        Try
            Dim clschkhistory As clsEdmUser = New clsEdmUser
            Dim clsCheckPwd As clsCheckPwd = New clsCheckPwd
            If (txtPassword.Text.Trim = "" Or txtRePassword.Text.Trim = "") Then
                strMsg = strMsg + String.Format(Resources.resValidateUser.NotPassword) + "\n"
                chkstatus = False
            Else
                If (txtPassword.Text.Length > 7) Then
                    If Not (txtPassword.Text.Trim = txtRePassword.Text.Trim) Then

                        strMsg = strMsg + String.Format(Resources.resValidateUser.NotPasswordMatch) + "\n"
                        chkstatus = False
                    End If
                Else
                    strMsg = strMsg + String.Format(Resources.resValidateUser.NotPasswordLong) + "\n"
                    chkstatus = False
                End If

                If Not (clsCheckPwd.checkpassword(txtPassword.Text)) Then
                    strMsg = strMsg + String.Format(Resources.resValidateUser.NotPasswordStrong) + "\n"
                    chkstatus = False
                End If


            End If
        Catch ex As Exception
            WriteError(ex, "chkPassword", "userEdit")
        End Try



        'If Not (hdfChangePwd.Value = txtPassword.Text.Trim) Then
        '    If Not (clschkhistory.chkPwdHistory(txtUserName.Text.Trim, txtPassword.Text.Trim)) Then
        '        strMsg = strMsg + String.Format(clschkhistory.Msg) + "\n"
        '        chkstatus = False
        '    End If
        'End If
        

        Return chkstatus
    End Function



    Private Sub SaveLogNew()
        Try
            Dim clsSessionMenuRight As clsSessionMenuRight = Session("ClsMenuRight")
            Dim desc As String = "บันทึก ข้อมูลผู้ใช้งาน (รหัสผู้ใช้งาน " + txtUserName.Text + ")"

            Dim clsLogTransaction As New clsLogTransaction
            clsLogTransaction.saveLogTransaction(txtUserName.Text, clsSessionMenuRight.getUserName, 3, desc, "M64")
            clsLogTransaction = Nothing
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub SaveLogEdit()
        Try
            Dim clsSessionMenuRight As clsSessionMenuRight = Session("ClsMenuRight")
            Dim desc As String = "บันทึก แก้ไข ข้อมูลผู้ใช้งาน (รหัสผู้ใช้งาน " + txtUserName.Text + ")"

            Dim clsLogTransaction As New clsLogTransaction
            clsLogTransaction.saveLogTransaction(txtUserName.Text, clsSessionMenuRight.getUserName, 4, desc, "M64")
            clsLogTransaction = Nothing
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

  


#End Region

    Protected Sub btnChkEmployee_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnChkEmployee.Click
        Try
            If txtUserName.Text = "" Then
                PanelUser.Enabled = False
                strMsg = "กรุณาระบุรหัสผู้เข้าใช้งาน"
                ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" + strMsg + "')", True)
            ElseIf txtUserName.Text.Length < 5 Then
                PanelUser.Enabled = False
                strMsg = "กรุณาระบุรหัสผู้เข้าใช้งาน อย่างน้อย 5 ตัวอักษร"
                ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" + strMsg + "')", True)
            Else
                sUserEntity = New UserEntities

                Dim qryUser = (From tblUser In sUserEntity.SUSER Where tblUser.USERNAME = txtUserName.Text.Trim).Count
                If (qryUser = 0) Then
                    btnChkEmployee.Enabled = False
                    btnOK.Enabled = True
                    txtUserName.Enabled = False
                    PanelUser.Enabled = True
                    strMsg = "รหัสผู้เข้าใช้งาน " & txtUserName.Text & " สามารถใช้งานได้"
                    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" + strMsg + "')", True)
                    ChkPageStatusBackEnablePanal(Request.QueryString("Status"))
                Else
                    PanelUser.Enabled = False
                    strMsg = "มีรหัสผู้เข้าใช้งาน " & txtUserName.Text & " อยู่ในระบบ"
                    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" + strMsg + "')", True)
                End If
                qryUser = Nothing
            End If
        Catch ex As Exception
            WriteError(ex, "btnChkEmployee_Click", "userEdit")
        End Try




    End Sub



    Protected Sub ddlDepartment_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDepartment.SelectedIndexChanged
        Try
            Dim clsDropDown As New clsMasterDropDown
            clsSessionMenuRight = Session("ClsMenuRight")
            If Not (ddlDepartment.SelectedValue = "0") Then
                ddlZone.Enabled = True
                ddlBranch.Enabled = True

                clsDropDown.setZoneToDdl(ddlZone, _
                                                  strDeptCode:=ddlDepartment.SelectedValue, _
                                                  strRoleCode:=clsSessionMenuRight.getRoleGroup, _
                                                  strZoneCode:=clsSessionMenuRight.getZoneCode)
                clsDropDown.setBranchUseAllCodeToDdl(ddlBranch, strDeptCode:=ddlDepartment.SelectedValue, zoneCodeUse:=ddlZone.SelectedValue, strBranchCode:=clsSessionMenuRight.getBranchCode, strRoleCode:=clsSessionMenuRight.getRoleGroup, strZoneCode:=clsSessionMenuRight.getZoneCode)
            Else
                ddlBranch.Enabled = False
                ddlZone.Enabled = False
                ddlBranch.SelectedValue = "0"
            End If


            clsDropDown = Nothing
        Catch ex As Exception
            WriteError(ex, "ddlDepartment_SelectedIndexChanged", "userEdit")
        End Try


    End Sub

    'Protected Sub ddlBranch_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlBranch.SelectedIndexChanged
    '    Dim clsDropDown As New clsMasterDropDown
    '    If Not (ddlBranch.SelectedIndex = 0) Then
    '        'clsDropDown.SetRoleGroupDDL(ddlRoleGroup, ddlBranch.SelectedValue)
    '    End If
    '    clsDropDown = Nothing
    'End Sub

    'Protected Sub txtPassword_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPassword.TextChanged
    '    hdfChangePwd.Value = 1
    '    txtRePassword.Focus()
    'End Sub

   
    'Protected Sub ddlRoleGroup_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlRoleGroup.SelectedIndexChanged

    'End Sub

    Protected Sub rdolIsPwdLock_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdolIsPwdLock.SelectedIndexChanged
        If (strStatusUsePage = "1") Then
            If (rdolIsPwdLock.SelectedValue = 0) Then
                Dim clsSessionMenuRight As clsSessionMenuRight = Session("ClsMenuRight")
                clsSystemConfig.setdefaultValueDate()
                clsSystemConfig.setdefaultValueString()

                If (clsSessionMenuRight.getRoleGroup < 50) Then
                    txtPwdExpireDate.Text = clsSystemConfig.PwdExpireDateUser
                Else
                    txtPwdExpireDate.Text = clsSystemConfig.PwdExpireDateAdmin
                End If

                txtPassword.Attributes.Add("value", clsEn.DecryPass(clsSystemConfig.Password))
                txtRePassword.Attributes.Add("value", clsEn.DecryPass(clsSystemConfig.Password))
                chkMustChangePwd.Checked = True
            End If
        End If
    End Sub

    Protected Sub rdolIsPwdExpire_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdolIsPwdExpire.SelectedIndexChanged
        If (strStatusUsePage = "3") Then
            If (rdolIsPwdExpire.SelectedValue = 0) Then
                Dim clsSessionMenuRight As clsSessionMenuRight = Session("ClsMenuRight")
                clsSystemConfig.setdefaultValueDate()
                clsSystemConfig.setdefaultValueString()

                If (clsSessionMenuRight.getRoleGroup < 50) Then
                    txtPwdExpireDate.Text = clsSystemConfig.PwdExpireDateUser
                Else
                    txtPwdExpireDate.Text = clsSystemConfig.PwdExpireDateAdmin
                End If

                txtPassword.Attributes.Add("value", clsEn.DecryPass(clsSystemConfig.Password))
                txtRePassword.Attributes.Add("value", clsEn.DecryPass(clsSystemConfig.Password))

                chkMustChangePwd.Checked = True
            End If

        End If
    End Sub

    Protected Sub rdolIsUserExpire_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdolIsUserExpire.SelectedIndexChanged
        If (strStatusUsePage = "2") Then
            If (rdolIsUserExpire.SelectedValue = 0) Then
                clsSystemConfig.setdefaultValueDate()

                txtStartDate.Text = clsSystemConfig.UserBeginDate
                txtEndDate.Text = clsSystemConfig.userExpireDate
            End If

        End If
    End Sub

    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload

    End Sub

    Protected Sub ddlRoleGroup_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlRoleGroup.SelectedIndexChanged
        Try
            Dim clsDropDown As New clsMasterDropDown
            clsSessionMenuRight = Session("ClsMenuRight")

            clsSystemConfig.setdefaultValueDate()
            If (ddlRoleGroup.SelectedValue < 50) Then
                txtPwdExpireDate.Text = clsSystemConfig.PwdExpireDateUser
            Else
                txtPwdExpireDate.Text = clsSystemConfig.PwdExpireDateAdmin
            End If

            If Not (ddlRoleGroup.SelectedValue = "0") Then
                ddlUserRight.Enabled = True
                'clsDropDown.setBranchUseAllCodeToDdl(ddlBranch, strDeptCode:=ddlDepartment.SelectedValue, strBranchCode:=clsSessionMenuRight.getBranchCode, strRoleCode:=clsSessionMenuRight.getRoleGroup, strZoneCode:=clsSessionMenuRight.getZoneCode)
                'clsDropDown.
                clsDropDown.setUserRightToDd(ddlUserRight, ddlRoleGroup.SelectedValue, "", ddlDepartment.SelectedValue)
            Else
                ddlUserRight.Enabled = False
                ddlUserRight.SelectedValue = "0"
            End If
        Catch ex As Exception
            WriteError(ex, "ddlRoleGroup_SelectedIndexChanged", "userEdit")
        End Try




    End Sub

    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
        clsSystemConfig = New clsSystemConfig
        clsSystemConfig.setdefaultValueDate()
        clsSystemConfig.setdefaultValueString()

        If (ddlRoleGroup.SelectedValue < 50) Then
            txtPwdExpireDate.Text = clsSystemConfig.PwdExpireDateUser
            txtPwdExpireDate0.Text = clsSystemConfig.PwdExpireDateUser

        Else
            txtPwdExpireDate.Text = clsSystemConfig.PwdExpireDateAdmin
            txtPwdExpireDate0.Text = clsSystemConfig.PwdExpireDateAdmin

        End If
        rdolIsPwdLock.SelectedValue = 0
        rdolIsPwdExpire.SelectedValue = 0

        txtPassword.Attributes.Add("value", clsSystemConfig.Password)
        txtRePassword.Attributes.Add("value", clsSystemConfig.Password)
        chkMustChangePwd.Checked = True

    End Sub

    Protected Sub ddlBranch_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlBranch.SelectedIndexChanged
        Dim clsDropDown As New clsMasterDropDown
        clsSessionMenuRight = Session("ClsMenuRight")

        If Not (ddlBranch.SelectedIndex = 0) Then
            Dim tempZoneCode As String = ddlBranch.SelectedValue.Split("/")(2)

            If Not (tempZoneCode = "ZZZ") Then

                clsDropDown.setRoleToDdl(ddlRoleGroup, "", clsSessionMenuRight.getRoleGroup, tempZoneCode)
            Else
                clsDropDown.setRoleToDdl(ddlRoleGroup, "", clsSessionMenuRight.getRoleGroup)
            End If

        End If
        

    End Sub

    Protected Sub ddlZone_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlZone.SelectedIndexChanged
        Dim clsDropDown As New clsMasterDropDown
        clsSessionMenuRight = Session("ClsMenuRight")
        If Not (ddlDepartment.SelectedValue = "0") Then
            clsDropDown.setBranchUseAllCodeToDdl(ddlBranch, strDeptCode:=ddlDepartment.SelectedValue, strBranchCode:=clsSessionMenuRight.getBranchCode, zoneCodeUse:=ddlZone.SelectedValue, strRoleCode:=clsSessionMenuRight.getRoleGroup, strZoneCode:=clsSessionMenuRight.getZoneCode)
            'clsDropDown.setZoneToDdl(ddlZone, _
            '                       strDeptCode:=ddlDepartment.SelectedValue, _
            '                       strRoleCode:=clsSessionMenuRight.getRoleGroup, _
            '                       strZoneCode:=clsSessionMenuRight.getZoneCode)
            'Else
            'ddlBranch.Enabled = False
            'ddlZone.Enabled = False
            'ddlBranch.SelectedValue = "0"
        End If
    End Sub
End Class

